package com.aj.http;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import android.content.Context;
import android.util.Log;

import com.aj.activity.R;
import com.aj.sharepreferences.LoveHomeSharedPreferences;
import com.aj.utils.MobileUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.entity.StringEntity;

public class AsynchronizationHttpRequest {
	private static final String TAG = AsynchronizationHttpRequest.class
			.getName();

	private static final String CLIENTERROR_STRING = "{\"returnCode\":\"0x99999\",\"serviceName\":\"clent_error\",\"result\":\",errorMsg\":\"%s\"}";
	private static final String WIFIRROR_STRING = "{\"returnCode\":999998,\"serviceName\":\"clent_error\",\"result\":\"\",errorMsg\":\"%s\"}";

	private static final int SUCCESS_CODE_INT = 200;

	public interface IHttpCallBack {
		public void success(String result);

		public void fail(String result);
	}

	public AsynchronizationHttpRequest() {

	}

	public void excute(Context context, String url,
			HashMap<String, String> paramsMap, IHttpCallBack httpCallBack) {
		excute(context, url, paramsMap, httpCallBack, false);
	}

	public void excute(Context context, String url,
			HashMap<String, String> paramsMap,
			final IHttpCallBack httpCallBack, boolean isNeedJudgeWifiFlag) {
		RequestParams params = new RequestParams(paramsMap);
		Log.e(TAG, "http request url:" + url);

		if (isNeedJudgeWifiFlag) {
			if (LoveHomeSharedPreferences.getWifiFlag()
					&& !MobileUtil.isWifiConnect(context)) {
				return;
			}
		}

		HttpUtils.asyncPost(url, params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					byte[] responseBody) {
				if (httpCallBack != null) {
					try {
						if (statusCode == SUCCESS_CODE_INT) {
							httpCallBack.success(new String(responseBody,
									DEFAULT_CHARSET));
						} else {
							httpCallBack.fail(String
									.format(CLIENTERROR_STRING,
											TAG
													+ " error from : Request Error ,statusCode:"
													+ statusCode));
						}
					} catch (UnsupportedEncodingException e) {
						httpCallBack.fail(String.format(CLIENTERROR_STRING, TAG
								+ " error from : UnsupportedEncodingException:"
								+ e.getLocalizedMessage()));
						e.printStackTrace();
					}
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					byte[] responseBody, Throwable error) {
				if (httpCallBack != null) {
					try {
						httpCallBack.fail(String.format(
								CLIENTERROR_STRING,
								TAG
										+ " Http Error:"
										+ new String(responseBody,
												DEFAULT_CHARSET)
										+ "  error message:"
										+ error.getLocalizedMessage()));
					} catch (UnsupportedEncodingException e) {
						httpCallBack.fail(String.format(CLIENTERROR_STRING, TAG
								+ " error from : UnsupportedEncodingException:"
								+ e.getLocalizedMessage()));
					}
				}
			}

			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}

	public void excute(Context context, String url, String paramsString,
			final IHttpCallBack httpCallBack) {
		excute(context, url, paramsString, httpCallBack, false);
	}

	public void excute(Context context, String url, String paramsString,
			final IHttpCallBack httpCallBack, boolean isNeedJudgeWifiFlag) {
		Log.e(TAG, "http request url:" + url);
		if (isNeedJudgeWifiFlag) {
			if (LoveHomeSharedPreferences.getWifiFlag()
					&& !MobileUtil.isWifiConnect(context)) {
				httpCallBack.fail(String.format(WIFIRROR_STRING, context
						.getResources().getString(R.string.wifi_error_text)));
				return;
			}
		}
		StringEntity stringEntity;
		try {
			stringEntity = new StringEntity(paramsString);
			HttpUtils.asyncPost(context, url, stringEntity, "application/json",
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] responseBody) {
							if (httpCallBack != null) {
								try {
									if (statusCode == SUCCESS_CODE_INT) {
										httpCallBack.success(new String(
												responseBody, DEFAULT_CHARSET));
									} else {
										httpCallBack.fail(String
												.format(CLIENTERROR_STRING,
														TAG
																+ " error from : Request Error ,statusCode:"
																+ statusCode));
									}
								} catch (UnsupportedEncodingException e) {
									httpCallBack.fail(String
											.format(CLIENTERROR_STRING,
													TAG
															+ " error from : UnsupportedEncodingException:"
															+ e.getLocalizedMessage()));
									e.printStackTrace();
								}
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] responseBody, Throwable error) {
							if (httpCallBack != null) {
								try {
									httpCallBack.fail(String.format(
											CLIENTERROR_STRING,
											TAG
													+ " Http Error:"
													+ new String(responseBody,
															DEFAULT_CHARSET)
													+ "  error message:"
													+ error.getLocalizedMessage()));
								} catch (UnsupportedEncodingException e) {
									httpCallBack.fail(String
											.format(CLIENTERROR_STRING,
													TAG
															+ " error from : UnsupportedEncodingException:"
															+ e.getLocalizedMessage()));
								}
							}
						}

						@Override
						public void onFinish() {
							super.onFinish();
						}
					});
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
	}
}
